
#include "../include/nosie.glsl"

attribute vec3 color;

varying vec3 vPosition;
varying vec3 vInstanceColor;

varying float npParam;
varying float waveParam;

varying float randomNum01;
varying float randomNum02;
varying float randomNum03;

uniform float uDistance;
uniform float uTime;
uniform float uPercent;
uniform float uVmin;

#define PI 3.1415926535

void main() {
    vInstanceColor = color;
    vPosition = position;
    vPosition.x = mod(vPosition.x + uDistance + 76.3, 152.6) - 76.3;

    //入场效果+流动效果
    vPosition = vPosition + (random21(position.xy + position.yz)-0.5) * (1. - uPercent) * 50.;
    // vPosition = vPosition + (cameraPosition+(random21(position.xy + position.yz)-0.5)*5. - vPosition) * (1. - uPercent); //* 50.;

    //流动参数
    float waveSize = 4.;
    float disToCenter = distance(vPosition, vec3(0.));
    float flow = mod(uTime, 2.5) * 40. - waveSize;
    waveParam = smoothstep(flow + waveSize, flow, disToCenter) * smoothstep(flow - waveSize, flow, disToCenter) * (1. - (flow + waveSize) / 100.);

    vec3 dirToCenter = normalize(vPosition - vec3(0.));
    vPosition += vec3(0., waveParam, 0.);

    //控制粒子停止移动
    waveSize = 20.;

    flow = mod(uTime, 2.5) * 40. - waveSize;
    float front = clamp(flow + waveSize, 0.002, 99999.);
    float back = clamp(flow - waveSize, 0., 99999.);
    float mid = clamp(flow, 0.001, 99999.);

    float kk = (1. - (flow + waveSize) / 100.);
    float ff = sqrt(smoothstep(front, mid, disToCenter));
    float bb = sqrt(smoothstep(back, mid, disToCenter));

    //更大的wave
    float np = ff * bb * kk;
    npParam = np;

    float randomNum = random21(position.xy + position.zz);
    vec3 randomVec = vec3(sin(uTime + randomNum * 23.324), cos(uTime + randomNum * 143.432), sin(uTime + randomNum * 576.423));
    vec3 moveAblePos = smoothstep(0.2 + 0.8 * np, 1., randomNum) * randomVec;
    // float wSize = SIZE - SIZE * (1. - np);
    float vMinSize = uVmin/9.5;
    float wSize = vMinSize - vMinSize * (1. - np) * 0.5;

    vPosition += moveAblePos * smoothstep(5., 50., disToCenter) * (1. - np) * 5.;

    randomNum01 = random21(vPosition.zz + vPosition.yx);
    randomNum02 = random21(vPosition.xx + vPosition.zy);
    randomNum03 = noise2d(vPosition.zy + vec2(0., vPosition.x * 0.02 + uTime));

    vec4 mvPosition = modelViewMatrix * vec4(vPosition, 1.0);
    gl_Position = projectionMatrix * mvPosition;
    gl_PointSize = clamp(wSize * (1. / - mvPosition.z),0.,vMinSize/10.);
}